package com.grape.bosfore.action;

import java.util.Date;
import java.util.UUID;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.grape.bosfore.action.common.BaseAction;
import com.grape.domain.base.Area;
import com.grape.domain.take_delivery.Order;

@Controller
@Namespace("/order")
@Scope("prototype")
@ParentPackage("json-default")
public class OrderAction extends BaseAction<Order>{
	private static final long serialVersionUID = 1L;
	
	@Action(value="save",results= {@Result(name="success",type="redirect",location="/index.html"),@Result(name="input",type="redirect",location="/index.html")})
	public String save() {
		// 开单三项,时间,状态,订单号
		model.setOrderTime(new Date());
		model.setStatus("1");
		model.setOrderNum(UUID.randomUUID().toString().replaceAll("-", ""));
		
		// 接受发信收信地址,查出来关联客户,交给bos_management
		String[] rec = recAreaInfo.split("/");
		String[] send = sendAreaInfo.split("/");
		
		Area recA = new Area();
		Area sendA = new Area();
		
		recA.setProvince(rec[0]);
		recA.setCity(rec[1]);
		recA.setDistrict(rec[2]);
		
		sendA.setProvince(send[0]);
		sendA.setCity(send[1]);
		sendA.setDistrict(send[2]);
		
		// 客户未登录使用-1
		Object cusId = ServletActionContext.getRequest().getSession().getAttribute("customerId");
		if (cusId==null) {
			cusId = -1;
		}
		
		model.setRecArea(recA);
		model.setSendArea(sendA);
		model.setCustomer_id(Integer.parseInt(cusId.toString()));
		
		System.out.println("第一站bos_fore,完成关联客户与初始收发地址: "+model);
		WebClient.create("http://localhost:8080/bos_management/faraway/orderService/save").accept(MediaType.APPLICATION_XML).put(model);
		
		return SUCCESS;
	}
	
	
	
	private String recAreaInfo;
	private String sendAreaInfo;

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
}
